namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class TabAssembler : AssemblerBaseUpdatable<KYO.DAL.Tab, TabCriteria, TabRowMap, TabDomainObjectFactory> {
        public TabAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.Tab ReadSingleByKey(int TabId) {
            TabCriteria criteria = new TabCriteria();
            criteria.Set_TabId(TabCriteria.AVAILABLE_OPERATORS.EQUALS, TabId);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.Tab PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }

            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                TabUpdateFactory updateFactory = new TabUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.CommandText = (comm.CommandText + "; SELECT SCOPE_IDENTITY();");
                    decimal NewIdentityKey = ((decimal)(comm.ExecuteScalar()));
                    PersistentObject.TabId = ((int)(NewIdentityKey));
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;
            if (cascade) {
                ModuleIntallAssembler tempModuleIntallAssembler = new ModuleIntallAssembler();

                if ((PersistentObject.IsModuleIntallCollNull() == false)) {
                    for (int i1 = 0; (i1 < PersistentObject.ModuleIntallColl.Count); i1 = (i1 + 1)) {
                        comm.Parameters.Clear();
                        tempModuleIntallAssembler.MakePersistent(PersistentObject.ModuleIntallColl[i1], cascade, comm);
                    }
                    PersistentObject.ModuleIntallColl.Clean();
                    this.InsertAddedModuleIntallCollMappingTable(PersistentObject, comm);
                    this.RemoveDeletedModuleIntallCollMappingTableEntries(PersistentObject, comm);
                    PersistentObject.ModuleIntallColl.Synch();
                }
                tempModuleIntallAssembler.Dispose();
            }
        }

        internal override void Delete(KYO.DAL.Tab PersistentObject, System.Data.Common.DbCommand comm) {
            this.DeleteModuleIntallCollMappingTable(PersistentObject, comm);

            comm.Parameters.Clear();

            TabSelectionFactory tempTabSelectionFactory = new TabSelectionFactory();
            string sql = "DELETE FROM [dbo].[Tab]";
            comm.CommandText = sql;
            tempTabSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Manage Mapping Tables
        private void InsertAddedModuleIntallCollMappingTable(KYO.DAL.Tab PersistentObject, System.Data.Common.DbCommand comm) {
            comm.CommandText = "IF NOT EXISTS (SELECT 1 FROM [dbo].[ModuleIntallForTab] (nolock) WHERE [ModuleInt" +
                "allId] = @rarg0 AND [TabId] = @larg1) INSERT INTO [dbo].[ModuleIntallForTab] ([M" +
                "oduleIntallId] , [TabId] ) VALUES (@rarg0, @larg1)";
            for (int i = 0; (i < PersistentObject.ModuleIntallColl.Added.Count); i = (i + 1)) {
                comm.Parameters.Clear();
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@larg1", PersistentObject.TabId);
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@rarg0", PersistentObject.ModuleIntallColl.Added[i].ID);
                comm.ExecuteNonQuery();
            }
        }

        private void DeleteModuleIntallCollMappingTable(KYO.DAL.Tab PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();
            comm.CommandText = "DELETE FROM [dbo].[ModuleIntallForTab] WHERE [TabId]=@arg1";
            ObjectPersistenceHelper.AddParameterWithValue(comm, "@arg1", PersistentObject.TabId);
            comm.ExecuteNonQuery();
        }

        private void RemoveDeletedModuleIntallCollMappingTableEntries(KYO.DAL.Tab PersistentObject, System.Data.Common.DbCommand comm) {
            comm.CommandText = "DELETE FROM [dbo].[ModuleIntallForTab] WHERE  [ModuleIntallId]=@rarg0 AND  [TabId" +
                "]=@arg1";
            for (int i = 0; (i < PersistentObject.ModuleIntallColl.Deleted.Count); i = (i + 1)) {
                comm.Parameters.Clear();
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@arg1", PersistentObject.TabId);
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@rarg0", PersistentObject.ModuleIntallColl.Deleted[i].ID);
                comm.ExecuteNonQuery();
            }
        }
        #endregion

        #region Manage Child Objects / Collections
        /// Reads the KYO.DAL.ModuleIntall from the database corresponding to the object keys passed as parameters
        // <param name="local">Corresponds to Tab attribute </param
        // <param name="localTabId">Corresponds to Tab attribute TabId</param
        public BusinessLayerHelper.PersistentList<KYO.DAL.ModuleIntall> GetModuleIntallColl(int localTabId) {
            BusinessLayerHelper.PersistentList<KYO.DAL.ModuleIntall> result = new BusinessLayerHelper.PersistentList<KYO.DAL.ModuleIntall>();
            System.Data.Common.DbConnection tempConnection = this.InstanceConnection;
            try {
                tempConnection.Open();
                System.Data.Common.DbCommand comm = tempConnection.CreateCommand();
                comm.CommandText = (ModuleIntallAssembler.SELECT + " FROM [dbo].[ModuleIntallForTab] INNER JOIN [dbo].[ModuleIntall] ON [dbo].[Module" +
                    "IntallForTab].[ModuleIntallId] = [dbo].[ModuleIntall].[ID] WHERE [dbo].[ModuleIn" +
                    "tallForTab].[TabId] = @arg1");
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@arg1", localTabId);
                ModuleIntallAssembler _tempAssem = new ModuleIntallAssembler();
                result.FillData(_tempAssem.ReadCollectionCustom(comm));
            }
            finally {
                tempConnection.Close();
            }
            return result;
        }
        #endregion

        #region Helper Utilities
        protected override TabRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            TabRowMap result = new TabRowMap();
            result.TabId = reader.GetInt32(reader.GetOrdinal("dboTabDOTTabId"));
            result.Name = reader.GetString(reader.GetOrdinal("dboTabDOTName"));
            result.ViewOder = reader.GetInt32(reader.GetOrdinal("dboTabDOTViewOder"));
            result.Published = reader.GetBoolean(reader.GetOrdinal("dboTabDOTPublished"));
            result.CSS = reader.GetString(reader.GetOrdinal("dboTabDOTCSS"));
            result.ParentId = reader.GetInt32(reader.GetOrdinal("dboTabDOTParentId"));
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + TabAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[Tab]";
            }
        }
        public const string RawFields = "[dbo].[Tab].[TabId] AS dboTabDOTTabId, [dbo].[Tab].[CSS] AS dboTabDOTCSS, [dbo].[" +
            "Tab].[Name] AS dboTabDOTName, [dbo].[Tab].[ParentId] AS dboTabDOTParentId, [dbo]" +
            ".[Tab].[Published] AS dboTabDOTPublished, [dbo].[Tab].[ViewOder] AS dboTabDOTVie" +
            "wOder";
        public const string SELECT = "SELECT [dbo].[Tab].[TabId] AS dboTabDOTTabId, [dbo].[Tab].[CSS] AS dboTabDOTCSS, " +
            "[dbo].[Tab].[Name] AS dboTabDOTName, [dbo].[Tab].[ParentId] AS dboTabDOTParentId" +
            ", [dbo].[Tab].[Published] AS dboTabDOTPublished, [dbo].[Tab].[ViewOder] AS dboTa" +
            "bDOTViewOder";
        #endregion
    }
}
